package com.cw.jeeyt.service.impl.mdata;

import java.util.List;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.dao.model.mdata.MdataBanksDo;
import com.cw.jeeyt.dao.query.mdata.MdataBanksQuery;
import com.cw.jeeyt.service.intf.mdata.MdataBanksService;
import com.cw.jeeyt.service.bo.mdata.MdataBanksBo;
import com.cw.jeeyt.service.converter.mdata.MdataBanksConverter;
import com.cw.jeeyt.dao.intf.mdata.MdataBanksDao;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 银行接口业务实现类
 *
 * @author admin
 * @date 2021-03-26 22:30:39
 */
@Service
public class MdataBanksServiceImpl implements MdataBanksService {
    @Autowired
    private MdataBanksDao mdataBanksDao;

    /**
     * 新增银行
     *
     * @param request 条件
     * @return Long
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insert(MdataBanksBo request) {
        if (request == null) {
            return null;
        }
        String id = SequenceTool.nextId();
        request.setBankId(id);
        request.setCreateTime(new Date());
        request.setUpdateTime(new Date());
        mdataBanksDao.insertBySelective(MdataBanksConverter.bo2Do(request));
        return id;
    }

    /**
     * 更新银行
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modify(MdataBanksBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateTime(new Date());
        mdataBanksDao.updateBySelective(MdataBanksConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除银行
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(MdataBanksBo request) {
        if (request == null) {
            return false;
        }
        return mdataBanksDao.deleteByKey(request.getBankId()) > 0;
    }

    /**
     * 分页查询银行
     *
     * @param request 条件
     * @return PageInfo<MdataBanksBo>
     */
    @Override
    public PageInfo<MdataBanksBo> selectPage(MdataBanksBo request, PageParam pageParam) {
        PageInfo<MdataBanksDo> pageInfo = mdataBanksDao.selectPage(MdataBanksQuery.builder()
            .bankId(request.getBankId())
            .bankName(request.getBankName())
            .bankNo(request.getBankNo())
            .bigLogo(request.getBigLogo())
            .clsbank(request.getClsbank())
            .dtype(request.getDtype())
            .remark(request.getRemark())
            .smallLogo(request.getSmallLogo())
            .status(request.getStatus())
            .build(), pageParam);
        List<MdataBanksBo> mdataBankss = MdataBanksConverter.doList2BoList(pageInfo.getList());
        return new PageInfo<>(pageInfo, mdataBankss);
    }

    /**
     * 查询银行详情信息
     *
     * @param request 条件
     * @return MdataBanksBo
     */
    @Override
    public MdataBanksBo queryDetail(MdataBanksBo request) {
        if (request == null) {
            return null;
        }
        return MdataBanksConverter.do2Bo(mdataBanksDao.selectByKey(request.getBankId()));
    }

    /**
     * 根据条件查询银行
     *
     * @param request 条件
     * @return List<MdataBanksBo>
     */
    @Override
    public List<MdataBanksBo> queryList(MdataBanksBo request) {
        if (request == null) {
            return null;
        }
        return MdataBanksConverter.doList2BoList(mdataBanksDao.selectByExample(MdataBanksQuery.builder()
            .bankId(request.getBankId())
            .bankName(request.getBankName())
            .bankNo(request.getBankNo())
            .bigLogo(request.getBigLogo())
            .clsbank(request.getClsbank())
            .dtype(request.getDtype())
            .remark(request.getRemark())
            .smallLogo(request.getSmallLogo())
            .status(request.getStatus())
            .build()));
    }

    /**
     * 根据key查询银行
     *
     * @param bankId
     * @return
     */
    @Override
    public MdataBanksBo selectByKey(String bankId) {

        return MdataBanksConverter.do2Bo(mdataBanksDao.selectByKey(bankId));
    }
}
